package com.uzai.console.web.profit;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.annotation.SystemLogType;
import com.uzai.console.dto.profit.nuserrewardrule.NuserRewardRuleIdDto;
import com.uzai.console.dto.profit.nuserrewardrule.NuserRewardRuleInfoDto;
import com.uzai.console.dto.profit.nuserrewardrule.NuserRewardRuleQuery;
import com.uzai.console.dto.profit.nuserrewardrulegroup.NuserRewardRuleGroupIdDto;
import com.uzai.console.dto.profit.nuserrewardrulegroup.NuserRewardRuleGroupInfoDto;
import com.uzai.console.dto.profit.nuserrewardrulegroup.NuserRewardRuleGroupQuery;
import com.uzai.console.entity.NuserRewardRuleGroup;
import com.uzai.console.enums.OperationTypeEnum;
import com.uzai.console.enums.SystemModelEnum;
import com.uzai.console.mapper.NuserRewardRuleGroupMapper;
import com.uzai.console.service.profit.NuserRewardRuleService;
import com.uzai.console.vo.base.SelectKeyVo;
import com.uzai.console.vo.profit.nuserrewardrule.NuserRewardRuleInfoVo;
import com.uzai.console.vo.profit.nuserrewardrulegroup.NuserRewardRuleGroupInfoVo;
import com.uzai.console.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuqi
 * @date 2021年08月25日 20:15
 */
@Api(value = "杀熟规则", tags = "分佣管理|杀熟规则")
@ApiSort(4)
@RestController
public class NuserRewardRuleContoller extends BaseController {
    private Logger logger = LoggerFactory.getLogger(NuserRewardRuleContoller.class);

    @Autowired
    private NuserRewardRuleService nuserrewardruleService;
    @Autowired
    private NuserRewardRuleGroupMapper nuserrewardruleGroupMapper;

    @ApiOperation(value = "分页查询杀熟规则列表", notes = "分页查询杀熟规则列表", response = NuserRewardRuleInfoVo.class)
    @ApiOperationSupport(order = 1)
    @PostMapping("/nuserrewardrule/findNuserRewardRuleList")
    public Object findNuserRewardRuleList(@RequestBody NuserRewardRuleQuery nuserrewardruleQuery) {
        return nuserrewardruleService.findNuserRewardRuleList(nuserrewardruleQuery);
    }

    @ApiOperation(value = "查询杀熟规则详情", notes = "查询杀熟规则详情", response = NuserRewardRuleInfoVo.class)
    @ApiOperationSupport(order = 2)
    @PostMapping("/nuserrewardrule/findNuserRewardRuleInfo")
    public Object findNuserRewardRuleInfo(@RequestBody NuserRewardRuleIdDto nuserrewardruleIdDto) {
        return nuserrewardruleService.findNuserRewardRuleInfo(nuserrewardruleIdDto);
    }

    @ApiOperation(value = "添加杀熟规则前检验接口", notes = "添加杀熟规则前检验接口")
    @ApiOperationSupport(order = 3)
    @PostMapping("/nuserrewardrule/addNuserRewardRuleUnit")
    public Object addNuserRewardRuleUnit() {
        NuserRewardRuleGroupQuery nuserRewardRuleGroupQuery = new NuserRewardRuleGroupQuery();
        nuserRewardRuleGroupQuery.setMerId(getLoginInfo().getMerId());
        List<NuserRewardRuleGroup> nuserRewardRuleGroupList = nuserrewardruleGroupMapper.findByList(nuserRewardRuleGroupQuery);
        if (nuserRewardRuleGroupList == null || nuserRewardRuleGroupList.size() == 0) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请先添加分组信息");
        }
        return true;
    }

    @SystemLogType(model = SystemModelEnum.NuserRewardRule, type = OperationTypeEnum.ADD)
    @ApiOperation(value = "添加杀熟规则", notes = "添加杀熟规则")
    @ApiOperationSupport(order = 3)
    @PostMapping("/nuserrewardrule/addNuserRewardRule")
    public Object addNuserRewardRule(@RequestBody NuserRewardRuleInfoDto nuserrewardruleInfoDto) {
        nuserrewardruleService.addNuserRewardRule(nuserrewardruleInfoDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.NuserRewardRule, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "修改杀熟规则", notes = "修改杀熟规则")
    @ApiOperationSupport(order = 4)
    @PostMapping("/nuserrewardrule/updateNuserRewardRule")
    public Object updateNuserRewardRule(@RequestBody NuserRewardRuleInfoDto nuserrewardruleInfoDto) {
        nuserrewardruleService.updateNuserRewardRule(nuserrewardruleInfoDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.NuserRewardRule, type = OperationTypeEnum.DELETE)
    @ApiOperation(value = "删除杀熟规则", notes = "删除杀熟规则")
    @ApiOperationSupport(order = 5)
    @PostMapping("/nuserrewardrule/deleteNuserRewardRule")
    public Object deleteNuserRewardRule(@RequestBody NuserRewardRuleIdDto nuserrewardruleIdDto) {
        nuserrewardruleService.deleteNuserRewardRule(nuserrewardruleIdDto);
        return true;
    }

    @ApiOperation(value = "分页查询杀熟规则分组列表", notes = "分页查询杀熟规则分组列表", response = NuserRewardRuleGroupInfoVo.class)
    @ApiOperationSupport(order = 6)
    @PostMapping("/nuserrewardrule/findNuserRewardRuleGroupList")
    public Object findNuserRewardRuleGroupList(@RequestBody NuserRewardRuleGroupQuery nuserrewardruleGroupQuery) {
        return nuserrewardruleService.findNuserRewardRuleGroupList(nuserrewardruleGroupQuery);
    }

    @SystemLogType(model = SystemModelEnum.NuserRewardRule, type = OperationTypeEnum.ADD)
    @ApiOperation(value = "添加杀熟规则分组", notes = "添加杀熟规则分组")
    @ApiOperationSupport(order = 7)
    @PostMapping("/nuserrewardrule/addNuserRewardRuleGroup")
    public Object addNuserRewardRuleGroup(@RequestBody NuserRewardRuleGroupInfoDto nuserrewardruleGroupInfoDto) {
        nuserrewardruleService.addNuserRewardRuleGroup(nuserrewardruleGroupInfoDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.NuserRewardRule, type = OperationTypeEnum.DELETE)
    @ApiOperation(value = "删除杀熟规则分组", notes = "删除杀熟规则分组")
    @ApiOperationSupport(order = 8)
    @PostMapping("/nuserrewardrule/deleteNuserRewardRuleGroup")
    public Object deleteNuserRewardRuleGroup(@RequestBody NuserRewardRuleGroupIdDto nuserrewardruleGroupIdDto) {
        nuserrewardruleService.deleteNuserRewardRuleGroup(nuserrewardruleGroupIdDto);
        return true;
    }

    @ApiOperation(value = "查询杀熟规则分组下拉列表框", notes = "查询杀熟规则分组下拉列表框", response = SelectKeyVo.class)
    @ApiOperationSupport(order = 9)
    @PostMapping("/nuserrewardrule/findNuserRewardRuleGroupSelect")
    public Object findNuserRewardRuleGroupSelect() {
        List<SelectKeyVo> selectIdVoList = new ArrayList<>();
        NuserRewardRuleGroupQuery nuserrewardruleGroupQuery = new NuserRewardRuleGroupQuery();
        nuserrewardruleGroupQuery.setMerId(getLoginInfo().getMerId());
        List<NuserRewardRuleGroup> nuserrewardruleGroupList = nuserrewardruleGroupMapper.findByList(nuserrewardruleGroupQuery);
        if (nuserrewardruleGroupList != null && nuserrewardruleGroupList.size() > 0) {
            for (NuserRewardRuleGroup nuserrewardruleGroup : nuserrewardruleGroupList) {
                SelectKeyVo selectKeyVo = new SelectKeyVo();
                selectKeyVo.setKey(String.valueOf(nuserrewardruleGroup.getId()));
                selectKeyVo.setValue(nuserrewardruleGroup.getName());
                selectKeyVo.setData(nuserrewardruleGroup.getRuleInfo());
                selectIdVoList.add(selectKeyVo);
            }
        }
        return selectIdVoList;
    }

    @SystemLogType(model = SystemModelEnum.NuserRewardRule, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "修改杀熟规则分组", notes = "修改杀熟规则分组")
    @ApiOperationSupport(order = 10)
    @PostMapping("/nuserrewardrule/updateNuserRewardRuleGroup")
    public Object updateNuserRewardRuleGroup(@RequestBody NuserRewardRuleGroupInfoDto nuserrewardruleGroupInfoDto) {
        nuserrewardruleService.updateNuserRewardRuleGroup(nuserrewardruleGroupInfoDto);
        return true;
    }

}
